package net.lainiao.mycost.controller;

import net.lainiao.mycost.bean.CostInfo;
import net.lainiao.mycost.service.ICostInfoService;
import net.lainiao.mycost.utils.Comm;
import net.lainiao.mycost.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Queue;

/**
 * Created by Administrator on 2017/3/22.
 */
@Controller
@RequestMapping("/cost/")
public class CostController extends Page {
    @RequestMapping("list.do")
    public String list(CostInfo info, Model model, HttpServletRequest request) {
        if (info == null) {
            info = new CostInfo();
        }
        info.setCostMark(Comm.MARK_YES);
        this.initPage(request);
        info.setStart(this.getPageNo());
        info.setLength(PAGE_NUM_BIG);
        model.addAttribute("list", costInfoService.list(info));
        model.addAttribute("info", info);
        return "cost/cost_list";
    }

    @RequestMapping("loadadd.do")
    public String loadadd(){
        return "cost/cost_add";
    }

    @RequestMapping("add.do")
    public String add(CostInfo info,Model model,HttpServletRequest request){
        if(info!=null){
            info.setCostMark(Comm.MARK_YES);
        }
        costInfoService.add(info);
        return list(null,model, request);
    }

    @RequestMapping("loadupdate.do")
    public String loadUpdate(CostInfo info,Model model){
        model.addAttribute("cost",costInfoService.getInfo(info));
        return "cost/cost_update";
    }

    @RequestMapping("update.do")
    public String update(CostInfo info,Model model,HttpServletRequest request){
        costInfoService.update(info);
        model.addAttribute("errorinfo","费用信息修改成功");
        return list(null,model,request);
    }

    @RequestMapping("delete.do")
    public String delete(Integer[] costIds,Model model,HttpServletRequest request){
        costInfoService.delete(costIds);
        model.addAttribute("errorinfo","费用信息删除成功");
        return list(null,model,request);
    }
}
